#pragma once

#include <memory>
#include <unordered_map>
#include <functional>

#include "network_message.h"

class session;

/* 单例类 */
class network_message_handler {
public:
    using network_message_handler_func =
        std::function<int(network_message_handler *, std::shared_ptr<session>, const network_message&)>;

    static network_message_handler& get_instance() {
        static network_message_handler instance;
        return instance;
    }

    int handle_message(std::shared_ptr<session> session, const network_message& msg);

private:
    network_message_handler()
    {
        init_message_handler_map();
    }

    network_message_handler(const network_message_handler&) = delete;
    network_message_handler& operator=(const network_message_handler&) = delete;

    int message_handler_heart_beat(std::shared_ptr<session> session, const network_message& msg);
    int message_handler_user_login(std::shared_ptr<session> session, const network_message& msg);
    int message_handler_user_register(std::shared_ptr<session> session, const network_message& msg);
    int message_handler_user_start_matching(std::shared_ptr<session> session, const network_message& msg);
    int message_handler_user_stop_matching(std::shared_ptr<session> session, const network_message& msg);
    int message_handler_left_button_clicked(std::shared_ptr<session> session, const network_message& msg);

    void register_message_handler(uint32_t msg_type, network_message_handler_func handler)
    {
        msg_handler_map_[msg_type] = handler;
    }

    /* 在此处添加消息类型和消息处理方式的映射关系 */
    void init_message_handler_map();

    /* data member */
    std::unordered_map<uint32_t, network_message_handler_func> msg_handler_map_;
};
